package com.example.cstvdemoone.fragments;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.etsy.android.grid.util.UgcAnimations;
import com.example.cstvdemoone.R;
import com.example.cstvdemoone.data.StatusResult;

public class User {
	private Context mContext;
	private Activity mActivity;
	private Application mKXApplication;
	private View mUser;
	private View mUserHead;
	private ImageView mHead_Wallpager;
	private ImageView mHead_Avatar;
	private TextView mHead_Name;
	private ImageView mHead_Gender;
	private TextView mHead_Constellation;
	private LinearLayout mHead_Sig_Layout;
	private TextView mHead_Sig;
	private TextView mHead_About;
	private TextView mHead_Photo;
	private TextView mHead_Diary;
	private TextView mHead_Friends;
	private LinearLayout mHead_Friends_List;
	private Button mHead_Friends_List_Count;
	private View mUgcView;
	private RelativeLayout mUgcLayout;
	private ImageView mUgc;
	private ImageView mUgcBg;
	private ImageView mUgcVoice;
	private ImageView mUgcPhoto;
	private ImageView mUgcRecord;
	private ImageView mUgcLbs;

	private Button mMenu;
	private ListView mDisplay;
	private UserInfoAdapter mAdapter;
	private boolean mUgcIsShowing = false;

	public User(Context context, Activity activity) {
		mContext = context;
		mActivity = activity;
		mUser = LayoutInflater.from(context).inflate(R.layout.user_info, null);
		mUserHead = LayoutInflater.from(context).inflate(
				R.layout.user_info_head, null);

		findViewById();
		setListener();
		init();
	}

	private void findViewById() {

		mHead_Wallpager = (ImageView) mUserHead
				.findViewById(R.id.user_info_head_wallpager);
		mHead_Avatar = (ImageView) mUserHead
				.findViewById(R.id.user_info_head_avatar);
		mHead_Name = (TextView) mUserHead
				.findViewById(R.id.user_info_head_name);
		mHead_Gender = (ImageView) mUserHead
				.findViewById(R.id.user_info_head_gender);
		mHead_Constellation = (TextView) mUserHead
				.findViewById(R.id.user_info_head_constellation);
		mHead_Sig_Layout = (LinearLayout) mUserHead
				.findViewById(R.id.user_info_head_sig_layout);
		mHead_Sig = (TextView) mUserHead.findViewById(R.id.user_info_head_sig);
		mHead_About = (TextView) mUserHead
				.findViewById(R.id.user_info_head_about);
		mHead_Photo = (TextView) mUserHead
				.findViewById(R.id.user_info_head_photo);
		mHead_Diary = (TextView) mUserHead
				.findViewById(R.id.user_info_head_diary);
		mHead_Friends = (TextView) mUserHead
				.findViewById(R.id.user_info_head_friends);
		mHead_Friends_List = (LinearLayout) mUserHead
				.findViewById(R.id.user_info_head_friends_list);
		mHead_Friends_List_Count = (Button) mUserHead
				.findViewById(R.id.user_info_head_friends_list_count);

		mUgcView = (View) mUser.findViewById(R.id.user_info_ugc);
		mUgcLayout = (RelativeLayout) mUgcView.findViewById(R.id.ugc_layout);
		mUgc = (ImageView) mUgcView.findViewById(R.id.ugc);
		mUgcBg = (ImageView) mUgcView.findViewById(R.id.ugc_bg);
		mUgcVoice = (ImageView) mUgcView.findViewById(R.id.ugc_voice);
		mUgcPhoto = (ImageView) mUgcView.findViewById(R.id.ugc_photo);
		mUgcRecord = (ImageView) mUgcView.findViewById(R.id.ugc_record);
		mUgcLbs = (ImageView) mUgcView.findViewById(R.id.ugc_lbs);

		mDisplay = (ListView) mUser.findViewById(R.id.user_info_display);
	}
	private void setListener() {
		mHead_Wallpager.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
			}
		});
		mHead_Avatar.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				new AlertDialog.Builder(mContext)
				.setTitle("请选择")
						.setItems(
								new String[] { "修改头像", "编辑我的资料", "设置为通讯录头像" },
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int which) {
										switch (which) {
										case 0:
											new AlertDialog.Builder(mContext)
											.setTitle("修改头像")
											.setItems(
													new String[] {
															"拍照上传",
															"上传手机中的照片" },
															new DialogInterface.OnClickListener() {

																public void onClick(
																		DialogInterface dialog,
																		int which) {
//																	Intent intent = null;
//																	switch (which) {
//																	case 0:
//																		intent = new Intent(
//																				MediaStore.ACTION_IMAGE_CAPTURE);
//																		File dir = new File(
//																				"/sdcard/KaiXin/Camera/");
//																		if (!dir.exists()) {
//																			dir.mkdirs();
//																		}
//																		mKXApplication.mUploadPhotoPath = "/sdcard/KaiXin/Camera/"
//																				+ UUID.randomUUID()
//																						.toString();
//																		File file = new File(
//																				mKXApplication.mUploadPhotoPath);
//																		if (!file
//																				.exists()) {
//																			try {
//																				file.createNewFile();
//																			} catch (IOException e) {
//
//																			}
//																		}
//																		intent.putExtra(
//																				MediaStore.EXTRA_OUTPUT,
//																				Uri.fromFile(file));
//																		mActivity
//																				.startActivityForResult(
//																						intent,
//																						ActivityForResultUtil.REQUESTCODE_UPLOADAVATAR_CAMERA);
//																		break;
//
//																	case 1:
//																		intent = new Intent(
//																				Intent.ACTION_PICK,
//																				null);
//																		intent.setDataAndType(
//																				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
//																				"image/*");
//																		mActivity
//																				.startActivityForResult(
//																						intent,
//																						ActivityForResultUtil.REQUESTCODE_UPLOADAVATAR_LOCATION);
//																		break;
//																	}
																}
															})
													.setNegativeButton(
															"取消",
															new DialogInterface.OnClickListener() {

																public void onClick(
																		DialogInterface dialog,
																		int which) {
																	dialog.cancel();
																}
															}).create().show();
											break;

										case 1:
//											// 璺宠浆鍒板叧浜庣晫闈�
//											Intent intent = new Intent();
//											intent.setClass(mContext,
//													AboutActivity.class);
//											intent.putExtra(
//													"result",
//													mKXApplication.mMyInfoResult);
//											mContext.startActivity(intent);
											break;
										case 2:
//											mContext.startActivity(new Intent(
//													mContext,
//													ContactsActivity.class));
											break;
										}
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int which) {
										dialog.cancel();
									}
								}).create().show();
			}
		});
		// 签名监听
		mHead_Sig_Layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// 跳转到修改签名界面
			}
		});
		// 最近来访数量监听
		mHead_Friends_List_Count.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// 跳转到最近来访列表界面
			}
		});
		// 关于监听
		mHead_About.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
			}
		});
		// 照片监听
		mHead_Photo.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
			}
		});
		// 日记监听
		mHead_Diary.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
			}
		});
		// 好友监听
		mHead_Friends.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
			}
		});
		// Path监听
		mUgcView.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// 判断是否已经显示,显示则关闭并隐藏
				if (mUgcIsShowing) {
					mUgcIsShowing = false;
					UgcAnimations.startCloseAnimation(mUgcLayout, mUgcBg, mUgc,
							500);
					return true;
				}
				return false;
			}
		});
		// Path监听
		mUgc.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// 判断是否显示,已经显示则隐藏,否则则显示
				mUgcIsShowing = !mUgcIsShowing;
				if (mUgcIsShowing) {
					UgcAnimations.startOpenAnimation(mUgcLayout, mUgcBg, mUgc,
							500);
				} else {
					UgcAnimations.startCloseAnimation(mUgcLayout, mUgcBg, mUgc,
							500);
				}
			}
		});
	}


	/**
	 * 界面初始化
	 */
	private void init() {
		getInfo();
		getVisitors();
		getStatus();
		mDisplay.addHeaderView(mUserHead);
		List<StatusResult> statusResults = new ArrayList<StatusResult>();
		StatusResult statusResult1 = new StatusResult();
		statusResult1.setName("张三");
		statusResult1.setContent("11111");
		statusResult1.setComment_count(10);
		statusResult1.setForward_count(50);
		statusResult1.setFrom("来自Android");
		statusResult1.setLike_count(40);
		statusResult1.setTime("2014-04-28");
		StatusResult statusResult2 = new StatusResult();
		statusResult2.setName("李四");
		statusResult2.setContent("222222222");
		statusResult2.setComment_count(10);
		statusResult2.setForward_count(50);
		statusResult2.setFrom("来自Android");
		statusResult2.setLike_count(40);
		statusResult2.setTime("2014-03-28");
		StatusResult statusResult3 = new StatusResult();
		statusResult3.setName("王五");
		statusResult3.setContent("555555555");
		statusResult3.setComment_count(10);
		statusResult3.setForward_count(50);
		statusResult3.setFrom("来自Android");
		statusResult3.setLike_count(40);
		statusResult3.setTime("2014-03-2");
		statusResults.add(statusResult1);
		statusResults.add(statusResult2);
		statusResults.add(statusResult3);
		mAdapter = new UserInfoAdapter(mContext,statusResults);
		mDisplay.setAdapter(mAdapter);
	}

	private void getInfo() {
	}

	private void getVisitors() {
	}

	private void getStatus() {
	}
	public void setWallpager() {
//		mHead_Wallpager.setImageBitmap(mKXApplication
//				.getTitleWallpager(mKXApplication.mWallpagerPosition));
	}

	public void setSignature(String arg0) {
//		mHead_Sig.setText(new TextUtil(mKXApplication).replace(arg0));
	}

	public void setAvatar(Bitmap bitmap) {
		mHead_Avatar.setImageBitmap(bitmap);
	}

	public boolean getUgcIsShowing() {
		return mUgcIsShowing;
	}
	public void closeUgc() {
		mUgcIsShowing = false;
	}

	public void showUgc() {
		if (mUgcView != null) {
			mUgcView.setVisibility(View.VISIBLE);
		}

	}
	public void dismissUgc() {
		if (mUgcView != null) {
			mUgcView.setVisibility(View.GONE);
		}
	}

	public View getView() {
		return mUser;
	}
	private class UserInfoAdapter extends BaseAdapter {
		private Context mContext;
		private List<StatusResult> mResults;

		public UserInfoAdapter(Context context, List<StatusResult> results) {
			mContext = context;
			mResults = results;
		}

		public int getCount() {
			return mResults.size();
		}

		public Object getItem(int position) {
			return mResults.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.user_info_item, null);
				holder = new ViewHolder();
				holder.avatar = (ImageView) convertView
						.findViewById(R.id.user_info_item_avatar);
				holder.name = (TextView) convertView
						.findViewById(R.id.user_info_item_name);
				holder.time = (TextView) convertView
						.findViewById(R.id.user_info_item_time);
				holder.content = (TextView) convertView
						.findViewById(R.id.user_info_item_content);
				holder.from = (TextView) convertView
						.findViewById(R.id.user_info_item_from);
				holder.comment_count = (TextView) convertView
						.findViewById(R.id.user_info_item_comment_count);
				holder.forward_count = (TextView) convertView
						.findViewById(R.id.user_info_item_forward_count);
				holder.like_count = (TextView) convertView
						.findViewById(R.id.user_info_item_like_count);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			StatusResult result = mResults.get(position);
//			Bitmap avatar = PhotoUtil.toRoundCorner(BitmapFactory
//					.decodeResource(mContext.getResources(), R.drawable.head),
//					15);
//			holder.avatar.setImageBitmap(avatar);
			holder.name.setText(result.getName());
			holder.time.setText(result.getTime());
			holder.content.setText(result.getContent());
			holder.from.setText(result.getFrom());
			holder.comment_count.setText(result.getComment_count() + "");
			holder.forward_count.setText(result.getForward_count() + "");
			holder.like_count.setText(result.getLike_count() + "");
			return convertView;
		}

		class ViewHolder {
			ImageView avatar;
			TextView name;
			TextView time;
			TextView content;
			TextView from;
			TextView comment_count;
			TextView forward_count;
			TextView like_count;
		}
	}

	private void PhotoDialog() {
		AlertDialog.Builder builder = new Builder(mContext);
		builder.setTitle("上传照片至开心网");
		builder.setItems(new String[] { "拍照上传", "上传手机中的照片" },
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						Intent intent = null;
						switch (which) {
						case 0:
//							intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//							File dir = new File("/sdcard/KaiXin/Camera/");
//							if (!dir.exists()) {
//								dir.mkdirs();
//							}
//							mKXApplication.mUploadPhotoPath = "/sdcard/KaiXin/Camera/"
//									+ UUID.randomUUID().toString();
//							File file = new File(
//									mKXApplication.mUploadPhotoPath);
//							if (!file.exists()) {
//								try {
//									file.createNewFile();
//								} catch (IOException e) {
//
//								}
//							}
//							intent.putExtra(MediaStore.EXTRA_OUTPUT,
//									Uri.fromFile(file));
//							mActivity
//									.startActivityForResult(
//											intent,
//											ActivityForResultUtil.REQUESTCODE_UPLOADPHOTO_CAMERA);
							break;

						case 1:
//							mContext.startActivity(new Intent(mContext,
//									PhoneAlbumActivity.class));
							break;
						}
					}
				});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		builder.create().show();
	}
}
